<?php

class ActiveRecord extends ArrayObject {
	public $table;
	private $crudObj;
	private $pk,$fk;
	private $status = 0; // (0) blank slaight (1) loaded data
	public $fields = array();
	
	public function __construct($table, $obj = NULL, $config = NULL, $exclude = NULL) {
		$this->table = $table;
		$this->crudObj = new Crud($table,$obj,$config,$exclude);
		$this->loadProperties();
	}
	
	private function loadProperties() {
		if(count($this->fields) < 1) {
			$fields = $this->crudObj->getShortFields();
		}
		foreach($fields as $f) {
			if($f != $this->crudObj->pk && $f != $this->crudObj->fk) {
				$this->fields[] = $f;
			}
			$this->$f = '';
		}
		$this->status = 0;
	}
	
	public function get() {
		$arr = array();
		foreach($this->fields as $f) {
			$arr[$f] = $this->$f;
		}
		if(count($arr) > 0) return $arr;
		return FALSE;
	}
	
	public function set(array $arr) {
		$success = FALSE;
		foreach($arr as $key => $val) {
			if($key == $this->crudObj->pk) {
				$this->pk = $val;
			} elseif($key == $this->crudObj->fk) {
				$this->fk = $val;
			} elseif(in_array($key,$this->fields)) {
				$this->$key = $val;
				$success = TRUE;
			}
		}
		return $success;
	}
	
	public function find_first() {
		if($res = $this->crudObj->find(array('order' => $this->crudObj->pk .' ASC', 'limit' => 1))) {
			$this->set($res);
			$this->status = 1;
			return TRUE;
		}
		return FALSE;
	}
	
	public function find_last() {
		if($res = $this->crudObj->find(array('order' => $this->crudObj->pk .' DESC', 'limit' => 1))) {
			$this->set($res);
			$this->status = 1;
			return TRUE;
		}
		return FALSE;
	}
	
	public function next() {
		if($this->status == 0) {
			return $this->find_first();
		} else {
			if($res = $this->crudObj->find(array('where' => $this->crudObj->pk .' = '. ($this->pk + 1)))) {
				$this->set($res);
				$this->status = 1;
				return TRUE;
			}
		}
		return FALSE;
	}
	
	
	public function prev() {
		$this->previous();
	}
	
	public function previous() {
		if($this->status == 0) {
			return $this->find_last();
		} else {
			if(($this->pk - 1) >= 0) {
				if($res = $this->crudObj->find(array('where' => $this->crudObj->pk .' = '. ($this->pk - 1)))) {
					$this->set($res);
					$this->status = 1;
					return TRUE;
				}
			}
		}
		return FALSE;
	}
	
	public function count() {
		if($res = $this->crudObj->find(array('select' => 'count(*) as myCount'))) return $res['myCount'];
		return FALSE;
	}
	
	/*
	public function findAll() {
		$this->status = 1;
	}
	
	public function current() {
		$arr = array();
		$arr = $this->get();
		if(count($arr) > 0) return $arr;
		return FALSE;
	}
	*/
	
	public function save() {
		$arr = array();
		$arr = $this->get();
		if(count($arr) < 1) return FALSE;
		if($this->status == 0) {
			if($lastId = $this->crudObj->add($arr)) {
				if(isset($this->crudObj->pk) && !empty($this->crudObj->pk)) 
				$this->pk = $lastId;
				$this->status = 1;
				return TRUE;
			}
		} elseif($this->status == 1 && isset($this->crudObj->pk) && !empty($this->crudObj->pk)) {
			if($this->crudObj->update($arr,array($this->crudObj->pk => $this->pk))) return TRUE;
		}
		return FALSE;
	}
	
	public function reset() {
		$this->loadProperties();
	}
	
	public function __call($m,$a) {
		return call_user_func_array(array($this->crudObj,$m),$a);
	}
	
}

?>